<?php
namespace App\Http\Resources\Base;

use App\Http\Resources\ResourceCollection;
use App\Services\Utils\TencentYun\ImageService;
use Illuminate\Support\Facades\App;

class UserResource extends ResourceCollection
{

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request            
     * @return array
     */
    public function toArray($request)
    {
        $data = $this->collection->shift();
        return self::makeDataDecode($data);
    }

    /**
     * 制作头像数据（一条）
     */
    public static function makeAvatarDecode($data)
    {
        // 头像
        if (isset($data['avatar']) && ! empty($data['avatar'])) {
            $data['avatar_view'] = ImageService::makeDownloadUri($data['avatar']);
        } else {
            $data['avatar'] = '';
            $data['avatar_view'] = ImageService::makeDownloadDefaultUri();
        }
        return $data;
    }

    /**
     * 制作登录数据（一条）
     */
    public static function makeSignDecode($data)
    {
        $userEntity = self::userEntity();
        
        if ($userEntity->isValid()) {
            $data['sign_in'] = 1;
        } else {
            $data['sign_in'] = 0;
            $data['sign_in_uri'] = $userEntity->getSignInUri();
        }
        return $data;
    }

    /**
     * 制作使用数据（一条）
     */
    public static function makeDataDecode($data)
    {
        // 头像
        $data = self::makeAvatarDecode($data);
        // 个人中心
        if (App::environment('master')) {
            $data['uri']['home'] = 'http://passport.tongwei.cn/User/index.html';
        } elseif (App::environment('dev')) {
            $data['uri']['home'] = 'http://test.tongwei.cn/Oauth/User/index.html';
        } else {
            $data['uri']['home'] = 'http://passport.tongwei.com/User/index.html';
        }
        // 店铺链接
        // if (isset($data['shop_id']) && ! empty($data['shop_id'])) {
        // $data['uri']['shop_supply'] = route('shopSupplyList', [
        // 'id' => $data['shop_id']
        // ]);
        // $data['uri']['shop_demand'] = route('shopDemandList', [
        // 'id' => $data['shop_id']
        // ]);
        // }
        return $data;
    }
}
